import { request } from '@/service/request';

/** 数据查询API */

/**
 * 执行查询
 */
export function executeQuery(data: Api.DataPlatform.QueryExecuteDTO) {
  return request<Api.DataPlatform.QueryResultDTO>({
    url: '/dataplatform/query/execute',
    method: 'post',
    data
  });
}

/**
 * 导出查询结果
 */
export function exportQueryResult(data: Api.DataPlatform.QueryExecuteDTO, format: string = 'CSV') {
  return request<Blob>({
    url: `/dataplatform/query/export?format=${format}`,
    method: 'post',
    data,
    responseType: 'blob'
  });
}

/**
 * 查询历史记录列表
 */
export function fetchQueryHistoryList(params?: Api.DataPlatform.QueryHistorySearchParams) {
  return request<Api.DataPlatform.QueryHistoryList>({
    url: '/dataplatform/query/history/list',
    method: 'get',
    params
  });
}

/**
 * 查询历史记录详情
 */
export function fetchQueryHistoryDetail(id: number) {
  return request<Api.DataPlatform.QueryHistory>({
    url: `/dataplatform/query/history/${id}`,
    method: 'get'
  });
}

/**
 * 删除历史记录
 */
export function deleteQueryHistory(ids: number[]) {
  return request({
    url: `/dataplatform/query/history/${ids.join(',')}`,
    method: 'delete'
  });
}

/**
 * 重新执行历史查询
 */
export function reExecuteHistory(id: number) {
  return request<Api.DataPlatform.QueryResultDTO>({
    url: `/dataplatform/query/history/${id}/reexecute`,
    method: 'post'
  });
}
